package cn.tedu.v6;

import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;


public class Client {
    private Socket socket;
    private static final String HOST = "127.0.0.1";
    private static final Integer PORT = 4545;
    private String ip;
    private BufferedReader br;
    private PrintWriter pw;

    public Client() {
        try {
            socket = new Socket(HOST, PORT);
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (NullPointerException e) {
            System.out.println("友情提示：IP传入错误导致连接失败~");
        } catch (UnknownHostException e) {
            System.out.println("友情提示：IP传入错误");
        } catch (ConnectException e) {
            System.out.println("客户端连接失败：建议检查IP或者端口号");
        } catch (IOException e) {
            System.out.println("客户端链接服务器失败，提示：检查IP/端口");
        }
    }

    public void start() {

        System.out.println("~~~欢迎使用xx牌QQ聊天室~~~");
        Scanner input = new Scanner(System.in);
        try {
            String severMessage = br.readLine();
            System.out.println(severMessage);
            String username = input.nextLine();
            pw.println(username);
            severMessage = br.readLine();
            System.out.println(severMessage);
            String password = input.nextLine();
            pw.println(password);
            severMessage = br.readLine();
            if (severMessage.startsWith("登录成功")) {
                chat();
            }
        } catch (IOException e) {
            System.out.println("客户端断开连接了！");
        } finally {
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            if (pw != null)
                pw.close();
            if (socket != null && !socket.isClosed()) {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("通讯关闭失败");
                }
            }
        }
    }


    private void chat() {
        new Thread(() -> {
            try {
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("服务器端说：" + message);
                    System.out.println("客户端请输入：");
                }
            } catch (IOException e) {
                System.out.println("服务器端读入失败了");
            }
        }).start();

        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("客户端请输入：");
            String say = input.nextLine();
            pw.println("客户端说：" + say);
        }
    }
        public static void main(String[] args) {
            new Client().start();
        }


}
